
def get_pdf_single_page(pdf_path, single_page_num=3):
    """
    获取pdf中某一页，读取时序号从0开始
    single_page_num=3,即第三页，序号从1开始
    """
        import PyPDF2
        import os     
        pdf_dir_path = os.path.dirname(pdf_path)
        f_pdf_rb = open(pdf_path, 'rb')
        pdf_reader = PyPDF2.PdfFileReader(f_pdf_rb)
        pdf_writer = PyPDF2.PdfFileWriter()
        split_pdf_name = "single_page_" + str(single_page_num) + ".pdf"
        pdf_writer.addPage(pdf_reader.getPage(single_page_num-1))
        split_pdf_path = os.path.join(pdf_dir_path, split_pdf_name)
        f_split_pdf_wb = open(split_pdf_path, 'wb')
        pdf_writer.write(f_split_pdf_wb)
        f_split_pdf_wb.close()    
        f_pdf_rb.close()
    
    
def get_pdf_multiple_page(pdf_path, page_range=(1, 3), null_page_num=0):
    """
    获取pdf中指定范围的页码内容
    page_range:指定范围，序号从1开始记
    null_page_num: 不计页码的数量
    """
    import PyPDF2
    import os     
    pdf_dir_path = os.path.dirname(pdf_path)
    pdf_name = os.path.basename(pdf_path)
    f_pdf_rb = open(pdf_path, 'rb')
    pdf_reader = PyPDF2.PdfFileReader(f_pdf_rb)
    pdf_writer = PyPDF2.PdfFileWriter()
    if null_page_num != 0:
        min_page, max_page = page_range[0]-null_page_num, page_range[1]-null_page_num
    else:
        min_page, max_page = page_range
    split_pdf_name = "_".join((pdf_name[:-4], str(min_page), str(max_page))) + ".pdf"
    for page_index in range(min_page-1, max_page):
        pdf_writer.addPage(pdf_reader.getPage(page_index))
    split_pdf_path = os.path.join(pdf_dir_path, split_pdf_name)
    f_split_pdf_wb = open(split_pdf_path, 'wb')
    pdf_writer.write(f_split_pdf_wb)
    f_split_pdf_wb.close()    
    f_pdf_rb.close() 
   
def pdf_split_process(pdf_path, split_page_list=[1, 3], null_page_num=0, split_pdf_name_list=None):
    """
    split_page_list: 分割的页码, 从1开始记, 与书上一致
    例如：[1, 3] # 总页码为4
        split_1: page:1, 2
        split_2: page:3, 4
    null_page_num: 不计页码的数量
    """
    import PyPDF2
    import os 
    from os.path import join as opj
    pdf_dir_path = os.path.dirname(pdf_path)
    f_pdf_rb = open(pdf_path, 'rb')
    pdf_reader = PyPDF2.PdfFileReader(f_pdf_rb)
    
    # 获取pdf总页数
    pdf_page_num = pdf_reader.getNumPages()
    # print(pdf_page_num)
    split_page_list.append(pdf_page_num+1)
    # 书籍常有不计页码的情况，那么需要去除不计页码的页数，
    # 才能与书籍的页码匹配
    # print(split_page_list)
    for i in range(len(split_page_list[:-1])):
        pdf_writer = PyPDF2.PdfFileWriter()
        split_min_page = split_page_list[i]-null_page_num-1
        split_max_page = split_page_list[i+1]-null_page_num-1
        if split_pdf_name_list == None:
            split_pdf_name = str(i+1) + ".pdf"
            split_pdf_name = "_".join((split_min_page, split_max_page)) + ".pdf"
        else:
            split_pdf_name = split_pdf_name_list[i] + ".pdf"
        for page_index in range(split_min_page, split_max_page):
            # print(split_min_page, split_max_page)
            # print(page_index)
            pdf_writer.addPage(pdf_reader.getPage(page_index))
        split_pdf_path = opj(pdf_dir_path, split_pdf_name)
        f_split_pdf_wb = open(split_pdf_path, 'wb')
        pdf_writer.write(f_split_pdf_wb)
        f_split_pdf_wb.close()
        
        # # 初始化报错, 直接每轮重新赋值: pdf_writer = PyPDF2.PdfFileWriter()
        # # return self._objects[ido.idnum - 1]
        # # IndexError: list index out of range
        # pdf_writer.__init__()
  
    f_pdf_rb.close()
    
            
pdf_path = "/root/family/python/python_pypdf/中医目诊理论发展与临床应用现状.PDF"
# get_pdf_single_page(pdf_path, single_page_num=4)
# get_pdf_multiple_page(pdf_path, page_range=(1, 3))
# pdf_split_process(pdf_path, split_page_list=[1, 3], null_page_num=0, split_pdf_name_list=None)



# 生成exe
# https://blog.csdn.net/qq_41485193/article/details/128171254


# pdf_file = open(pdf_path, 'rb')
# 等价于pdf_path
# print(pdf_file.name)
